package com.base.common.view.base;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.base.common.R;
import com.base.common.databinding.BaseTitleActivityBinding;
import com.base.common.databinding.BaseTitleToolbarBinding;
import com.base.common.utils.ColorUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.view.roundview.RoundTextView;
import com.base.common.viewmodel.BaseViewModel;


/**
 * @author sugar
 * @date 2018/11/2
 */

public abstract class BaseTitleActivity<DB extends ViewDataBinding, VM extends BaseViewModel> extends BaseActivity<DB, VM> {

    protected BaseTitleToolbarBinding mTitleBinding;
    protected BaseTitleActivityBinding baseTitleActivityBinding;

    @Override
    protected void initContentView(@LayoutRes int layoutResID) {
        baseTitleActivityBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.base_title_activity, null, false);

        binding = DataBindingUtil.inflate(getLayoutInflater(), layoutResID, null, false);
        binding.setLifecycleOwner(this);

        baseTitleActivityBinding.flContainer.addView(binding.getRoot());
        setContentView(baseTitleActivityBinding.getRoot());
        mTitleBinding = baseTitleActivityBinding.inToolbar;

        mTitleBinding.ivImageLeftClose.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                finish();
            }
        });
        mTitleBinding.clToolbar.setBackgroundResource(getStatusBarColor());
        initToolBar(mTitleBinding);
    }


    public void initToolBar(BaseTitleToolbarBinding mTitleBinding) {

    }


    public void setToolBarBack(@ColorRes int colorRes) {
        mTitleBinding.clToolbar.setBackgroundColor(ColorUtils.getColor(colorRes));
    }

    public void setVisibilityIvClose(boolean isShow) {
        if (isShow) {
            mTitleBinding.ivImageLeftClose.setVisibility(View.VISIBLE);
            mTitleBinding.tvTextLeft.setVisibility(View.GONE);
        } else mTitleBinding.ivImageLeftClose.setVisibility(View.GONE);
    }

    public void setVisibilityTextLeft(boolean isShow) {
        if (isShow) {
            mTitleBinding.ivImageLeftClose.setVisibility(View.GONE);
            mTitleBinding.tvTextLeft.setVisibility(View.VISIBLE);
        } else mTitleBinding.ivImageLeftClose.setVisibility(View.GONE);
    }

    public void setTextLeft(String text) {
        mTitleBinding.tvTextLeft.setText(text);
    }

    public void setTitle(String title,@ColorRes int colorId)
    {
        mTitleBinding.tvTitleCenter.setText(title);
        mTitleBinding.tvTitleCenter.setTextColor(ContextCompat.getColor(this,colorId));
    }

    public void setTitle(String title)
    {
        mTitleBinding.tvTitleCenter.setText(title);
    }

    public RoundTextView getTextRight() {
        return mTitleBinding.tvTextRight;
    }

    public void setRightText(String text) {
        mTitleBinding.tvTextRight.setText(text);
    }

    public void setRightText(String text ,@ColorRes int colorId)
    {
        mTitleBinding.tvTextRight.setText(text);
        mTitleBinding.tvTextRight.setTextColor(ContextCompat.getColor(this,colorId));
    }

    public void setRightTextOnClick(View.OnClickListener onClick) {
        mTitleBinding.tvTextRight.setOnClickListener(onClick);
    }

    public void setCloseImg(@DrawableRes int resId)
    {
        mTitleBinding.ivImageLeftClose.setImageResource(resId);
    }


    public void setRightImg(int resId) {
        mTitleBinding.tvTextRight.setVisibility(View.GONE);
        mTitleBinding.ivTextRight.setVisibility(View.VISIBLE);
        mTitleBinding.ivTextRight.setImageResource(resId);
    }

    /**
     * 设尾部分隔线是否显示
     *
     * @param isShow
     */
    public void setBottomLineVisibility(boolean isShow) {
        int visibility = isShow ? View.VISIBLE : View.GONE;
        baseTitleActivityBinding.vBottomLine.setVisibility(visibility);
    }

    public View getBottomView() {
        return baseTitleActivityBinding.llBottom;
    }

}
